<template>
  <div class="search-form">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="运单编号" prop="billNo">
        <el-input v-model="queryParams.billNo" placeholder="请输入运单编号" clearable style="width: 200px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="运单状态" prop="billStatus">
        <el-select v-model="queryParams.billStatus" placeholder="请选择运单状态" clearable style="width: 150px">
          <el-option v-for="option in dict.type.bill_status" :key="option.value" :label="option.label"
            :value="option.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="发货方" prop="senderName">
        <el-input v-model="queryParams.senderName" placeholder="请输入发货方名称" clearable style="width: 150px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="收货方" prop="receiverName">
        <el-input v-model="queryParams.receiverName" placeholder="请输入收货方名称" clearable style="width: 150px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间" prop="dateRange">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { shipmentStatusOptions } from '../api/comprehensive'

export default {
  name: 'SearchForm',
  dicts: ['bill_status', 'transport_type'],
  props: {
    // 显示搜索条件
    showSearch: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billNo: undefined,
        billStatus: undefined,
        senderName: undefined,
        receiverName: undefined,
        beginTime: undefined,
        endTime: undefined,
        sortField: undefined,
        sortOrder: undefined
      },
      // 日期范围
      dateRange: [],
      // 状态选项
      statusOptions: shipmentStatusOptions
    }
  },
  watch: {
    // 监听日期范围变化
    dateRange (val) {
      if (val && val.length === 2) {
        this.queryParams.beginTime = val[0]
        this.queryParams.endTime = val[1]
      } else {
        this.queryParams.beginTime = undefined
        this.queryParams.endTime = undefined
      }
    }
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.$emit('query', { ...this.queryParams })
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 重置表单
    resetForm (refName) {
      if (this.$refs[refName]) {
        this.$refs[refName].resetFields()
      }
    },
    /** 获取查询参数 */
    getQueryParams () {
      return { ...this.queryParams }
    },
    /** 设置查询参数 */
    setQueryParams (params) {
      Object.assign(this.queryParams, params)
      if (params.beginTime && params.endTime) {
        this.dateRange = [params.beginTime, params.endTime]
      }
    },
    /** 更新排序参数 */
    updateSortParams (sortField, sortOrder) {
      this.queryParams.sortField = sortField
      this.queryParams.sortOrder = sortOrder
    }
  }
}
</script>

<style lang="scss" scoped>
.search-form {
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  margin-bottom: 10px;

  .el-form--inline .el-form-item {
    margin-right: 20px;
    margin-bottom: 15px;
  }
}
</style>
